import request from "@/components/request/index";

/**
 *
 * 其他
 */

/**
 * 文件上传
 * /file/upload
 * @param data
 * @returns {*}
 */
export const upload = (data) => {
    return request({
        url: '/file/upload',
        method: "POST",
        headers: {
            "content-type": "multipart/form-data",
            // response.setHeader("Access-Control-Allow-Origin","*");
        },
        data
    });
};

/**
 * ######################################################################################
 * mongodb
 */
/**
 * 根据id查询数据
 * post 请求
 * @param data 参数
 *      * _id 主键
 *      * source 表名
 */
export const findById = (data) => {
    return request({
        url: '/comm/findById',
        method: "POST",
        headers: {
            "content-type": "application/json;charset=UTF-8",
            // response.setHeader("Access-Control-Allow-Origin","*");
        },
        data
    });
};

/**
 * ######################################################################################
 * mysql
 */
/**
 * /comm/selectByCustomCondition
 */
export const selectCustomCondition = (key, data) => {
    return request({
        url: '/comm/selectByCustomCondition/' + key,
        method: "POST",
        headers: {
            "content-type": "application/json;charset=UTF-8",
            // response.setHeader("Access-Control-Allow-Origin","*");
        },
        data
    });
}

/**
 * 自定接口方法
 */
export const customInterface = (url, data) => {
    return request({
        url: '/' + url,
        method: "POST",
        headers: {
            "content-type": "application/json;charset=UTF-8",
            // response.setHeader("Access-Control-Allow-Origin","*");
        },
        data
    });
}

/**
 * /comm/one/selectById
 */
export const mSelectById = (key, data) => {
    return request({
        url: '/comm/one/selectById/' + key,
        method: "POST",
        headers: {
            "content-type": "application/json;charset=UTF-8",
            // response.setHeader("Access-Control-Allow-Origin","*");
        },
        data
    });
}
/**
 * /comm/one/deleteByIds
 */
export const mDeleteByIds = (key, data) => {
    return request({
        url: '/comm/one/deleteByIds/' + key,
        method: "POST",
        headers: {
            "content-type": "application/json;charset=UTF-8",
            // response.setHeader("Access-Control-Allow-Origin","*");
        },
        data
    });
}
/**
 * /comm/one/deleteById
 */
export const mDeleteById = (key, data) => {
    return request({
        url: '/comm/one/deleteById/' + key,
        method: "POST",
        headers: {
            "content-type": "application/json;charset=UTF-8",
            // response.setHeader("Access-Control-Allow-Origin","*");
        },
        data
    });
}

/**
 * js
 * ############################################################################################################################
 */


/**
 * 随机生成字符串
 * @param len 指定生成字符串长度
 */
export const getRandomString = (len) => {
    let _charStr = 'abacdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789',
        min = 0,
        max = _charStr.length - 1,
        _str = '';                    //定义随机字符串 变量
    //判断是否指定长度，否则默认长度为15
    len = len || 15;
    //循环生成字符串
    for (var i = 0, index; i < len; i++) {
        index = (function (randomIndexFunc, i) {
            return randomIndexFunc(min, max, i, randomIndexFunc);
        })(function (min, max, i, _self) {
            let indexTemp = Math.floor(Math.random() * (max - min + 1) + min),
                numStart = _charStr.length - 10;
            if (i == 0 && indexTemp >= numStart) {
                indexTemp = _self(min, max, i, _self);
            }
            return indexTemp;
        }, i);
        _str += _charStr[index];
    }
    return _str;
}


/**
 * list
 * @param list 集合
 * @param type eq 等于的字段
 * @param key 集合中的字段
 * @returns {*} 筛选后集合
 */
export const listFilter = (list, eq, key = 'position') => {
    if (list === undefined || list === [] || list === null) {
        return [];
    }
    return list.filter(en => {
        return eq === en[key];
    })
}
/**
 *
 * @param arr 需要处理的集合 无论多少层 【 【{},{}】】
 * @param keyField 需要处理的字段 id
 * @param valueField 需要赋值的字段 value
 * @param obj
 * @returns {*[]}
 */
export const setListElement = (arr, keyField, valueField, obj) => {
    // console.log('setListElement',obj)
    if (arr === undefined || arr === [] || arr === null) {
        return [];
    } else {
        if (Array.isArray(arr)) {

            arr.forEach(item => {
                if (Array.isArray(item)) {
                    setListElement(item, keyField, valueField, obj);
                } else if (item && typeof item === 'object') {
                    // console.log('item[keyField]',item[keyField])
                    // console.log('item[valueField]', item[valueField])
                    // console.log('obj[item[keyField]]', obj[item[keyField]])
                    if (item[keyField] && obj[item[keyField]]) {
                        item[valueField] = obj[item[keyField]];
                    }
                }
            });
        }
    }
}
/**
 *
 * @param arr 需要处理的集合 无论多少层 【 【{},{}】】
 * @param keyField 需要处理的这段
 * @param valueField 需要赋值的这段
 * @param obj
 * @returns  {}
 */
export const getListElementToObj = (arr, keyField, valueField, obj) => {
    if (arr === undefined || arr === [] || arr === null) {
        return {};
    } else {
        if (Array.isArray(arr)) {

            arr.forEach(item => {
                if (Array.isArray(item)) {

                    getListElementToObj(item, keyField, valueField, obj);
                } else if (item && typeof item === 'object') {
                    obj[item[keyField]] = item[valueField]
                }
            });
        }
    }
}

/**
 * 返回新的集合
 * @param arr
 * @param key
 * @returns {*[]}
 */
export const getKeyList = (arr, key) => {
    if (arr === undefined || arr === [] || arr === null) {
        return [];
    } else {
        let restList = [];
        arr.forEach(item => {
            if (item[key]) {
                restList.push(item[key]);
            }
        });
        return restList;
    }
}
/**
 * 解析搜索条件拼接到parMap中
 * @param parMap
 * @param template
 */
export const setTableSearch = (parMap, template) => {

    // console.log('template', template)
    let andStr = parMap['andStr'] ? parMap['andStr'] : [];
    if (template.result_columns && template.result_columns != undefined) {
        listFilter(template.result_columns, true, "isBaseSearch")
            .forEach(item => {
                console.log('item', item)
                if (item.searchValue && item.searchValue !== null && item.searchValue !== "") {
                    let parItem = {
                        "key": item.key,
                        "value": item.searchValue,
                        "type": "like"
                    }
                    andStr.push(parItem)
                }
            })
        parMap['andStr'] = andStr;
        console.log('parMap', parMap)
    }
    return parMap;
}












